//
//  PTTabBarManager.m
//  封装TableBar
//
//  Created by 贾旭周 on 15/12/11.
//  Copyright © 2015年 gm-iMac-iOS-03. All rights reserved.
//

#import "TPTabBarManager.h"
#import "TPNavigationController.h"
#import "TPTooBar.h"




static TPTabBarManager *manager = nil;


@interface TPTabBarManager ()
<GMBottomViewIndexDelegate>
{
    NSInteger curNavControllerIndex; // 当前NavController的顺序，从0开始
    /// footbar
    TPTooBar *toorBarView_;

}

/// 所有导航Nav
@property (nonatomic, strong) NSMutableArray *controllers;


@end


@implementation TPTabBarManager

#pragma mark- ************************初始化********************************
/********************************初始化************************************/

+ (id)share
{
    if (manager == nil)
    {
        manager = [[TPTabBarManager alloc] init];
    }

    return manager;
}

- (void)setToorBarTitleAry:(NSArray *)aryTitle UnSelectImageAry:(NSArray *)aryUnSelectAry selectImageAry:(NSArray *)arySelectAry
{
    [self initBottomViewWithTitleAry:aryTitle unSelectImageAry:aryUnSelectAry selectImageAry:arySelectAry];
}

- (void)setVCArray:(NSArray *)aryVC
{
    if (aryVC.count <=0)
    {
        return;
    }
    _controllers = [aryVC mutableCopy];
    
    [self initViewControllers];     //初始化ViewControllers
    
}

- (void)setToorBarBackForColor:(UIColor *)color
{
    NSAssert(toorBarView_ != nil, @"ToorBar不能为空");
    
    toorBarView_.backgroundColor = color;
}

- (void)setToorBarBackForImage:(UIImage *)img
{
    NSAssert(toorBarView_ != nil, @"ToorBar不能为空");

    [toorBarView_ setImage:img];
}

- (id)init
{
    if (self = [super init])
    {
        [self initVariable];
    }
    return self;
}

- (void)initVariable
{
    curNavControllerIndex = 0;
    _controllers = [NSMutableArray arrayWithCapacity:0];
}

- (void)initViewControllers
{
    TPNavigationController *nav = [_controllers objectAtIndex:0];
    UIViewController *rootVC = [[UIViewController alloc] init];
    self.delegateWindow.rootViewController = rootVC;
    [self.delegateWindow.rootViewController.view addSubview:nav.view];
    [self.delegateWindow.rootViewController.view bringSubviewToFront:toorBarView_];
}

- (void)initBottomViewWithTitleAry:(NSArray *)aryTitle unSelectImageAry:(NSArray *)aryUnSelect selectImageAry:(NSArray *)arySelectAry
{
    CGFloat fltScreenH = [UIScreen mainScreen].bounds.size.height;
    CGFloat fltScreenW = [UIScreen mainScreen].bounds.size.width;

    toorBarView_ = [[TPTooBar alloc] initWithFrame:CGRectMake(0, fltScreenH - 49, fltScreenW, 49)];
    /// 没有标题传毛线Title 草泥马，给我看的么？楼下小黑
    toorBarView_.aryTitle = aryTitle;
    toorBarView_.aryUnSelectImages = aryUnSelect;
    toorBarView_.arySelectImages = arySelectAry;

    toorBarView_.delegate = self;
    [toorBarView_ drawNecessaryView];

    [self.delegateWindow.rootViewController.view addSubview:toorBarView_];
    [toorBarView_ setBottomBtnClicked:BottomIndex_A];
    
    
    
    
}

#pragma mark- ***********************set方法*******************************
/*******************************set方法************************************/

- (UIWindow *)delegateWindow
{
    if (!_delegateWindow)
    {
        _delegateWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [_delegateWindow makeKeyAndVisible];
    }

    return _delegateWindow;
}

- (TPNavigationController *)curNavController
{
    TPNavigationController *navController = [self.controllers objectAtIndex:curNavControllerIndex];
    return navController;
}

- (TPNavigationController *)getNavControllerByIndex:(BottomIndex)index
{
    if (self.controllers.count > index)
    {
        TPNavigationController *navController = [self.controllers objectAtIndex:index];
        return navController;
    }
    else
    {
        return nil;
    }
 
}


- (NSInteger)getLastIndex
{
    return curNavControllerIndex;
}

#pragma mark- ***********************Function******************************
/*****************************Function************************************/

/// TableView回到顶部 或其他一些操作
- (void)repeatClick:(NSInteger)index
{

}

- (void)jumpToNav:(BottomIndex)bottomIndex
{
    [toorBarView_ setBottomBtnClicked:bottomIndex];
}

- (void)showBottomView:(BOOL)show animation:(BOOL)animation
{
    CGFloat duration = 0.3; // 动画间隔时间

    if (!animation) // 无动画
    {
        duration = 0;
    }

    if (show) // 显示Bottom
    {
        toorBarView_.hidden = NO;
    }

    [UIView animateWithDuration:duration animations:^{

        CGFloat fltScreenH = [UIScreen mainScreen].bounds.size.height;
        CGFloat fltScreenW = [UIScreen mainScreen].bounds.size.width;
        toorBarView_.frame = CGRectMake(0,
                                       show?fltScreenH-toorBarView_.bounds.size.height:fltScreenH+20,
                                       fltScreenW,
                                       toorBarView_.bounds.size.height);
    } completion:^(BOOL finished) {
        toorBarView_.hidden = !show;
    }];
}


#pragma mark- ***********************代理方法******************************
/*******************************代理方法************************************/


- (void)bottomViewIndexDelegate:(BottomIndex)bottomIndex lastSelectedTabIndex:(BottomIndex)lastIndex
{
    TPNavigationController *navi = (TPNavigationController *)[self.controllers objectAtIndex:bottomIndex];

    if (bottomIndex == curNavControllerIndex)
    {
        [navi popToRootViewControllerAnimated:NO];
        [self repeatClick:bottomIndex];
    }
    else
    {
        [self.delegateWindow.rootViewController.view insertSubview:navi.view belowSubview:toorBarView_];
        [self.curNavController.view removeFromSuperview];
    }

    curNavControllerIndex = bottomIndex;

    if ([_delegate respondsToSelector:@selector(selectIndex:lastIndex:)])
    {
        [_delegate selectIndex:bottomIndex lastIndex:lastIndex];
    }
}


@end
